package com.ct.sales.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;
import com.ct.sales.R;

import java.util.HashSet;

/**
 * RadiateView
 * User: Twinkle
 * Date: 13-1-16
 */
public class RadiateView extends View {

    private View mVwMain;
    private HashSet<View> mVwAssistSet = new HashSet<View>();

    public RadiateView(Context context) {
        super(context);
    }

    public RadiateView(Context context, AttributeSet attr) {
        super(context, attr);
    }

    public void setMainView(View v) {
        mVwMain = v;
    }

    private Point getViewPoint(View v) {
        return new Point(v.getLeft() + (v.getWidth() >> 1), v.getTop() + (v.getHeight() >> 1));
    }

    public void addAssistView(View v) {
        mVwAssistSet.add(v);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mVwMain == null || mVwAssistSet.size() <= 0) {
            return;
        }

        Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.line_green));
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(10);
        //canvas.drawLine(0, 0, 100, 100, paint);

        Point p = getViewPoint(mVwMain);
        int xDraw = p.x;
        int yDraw = p.y;
        for (View v : mVwAssistSet) {
            Point targetPoint = getViewPoint(v);
            canvas.drawLine(xDraw, yDraw, targetPoint.x, targetPoint.y, paint);
        }

    }

}
